package com.mobile.check.services;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

import cn.droidlover.xdroidmvp.event.BusProvider;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import com.mobile.check.beans.TaskItemInfo;
import com.mobile.check.db.EntityManager;
import com.mobile.check.events.SaveExcelEvent;

public class SaveExcelService extends Service {

    Disposable receiveDisposable;

    @Override
    public void onCreate() {
        super.onCreate();
        registSubmitEvent();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void registSubmitEvent() {
        receiveDisposable = BusProvider.getBus().toObservable(SaveExcelEvent.class).subscribe(new Consumer<SaveExcelEvent>() {
            @Override
            public void accept(SaveExcelEvent dataEvent) throws Exception {
                saveExcel(dataEvent.getItemInfo(),dataEvent.getTaskType(), dataEvent.getType());
            }
        });
    }

    @Override
    public void onDestroy() {
        receiveDisposable.isDisposed();
        super.onDestroy();
    }

    private void saveExcel(final TaskItemInfo itemInfo, final int taskType, final int type) {
        Observable.just(itemInfo).map(new Function<TaskItemInfo, TaskItemInfo>() {
            @Override
            public TaskItemInfo apply(TaskItemInfo info) throws Exception {

                FileInputStream inputStream;
                XSSFSheet sheet;
                XSSFWorkbook workbook;
                FileOutputStream fileOut;

                EntityManager.getInstance().saveTaskItemInfo(itemInfo);
                inputStream = new FileInputStream(itemInfo.getFilePath());
                workbook = new XSSFWorkbook(inputStream);
                sheet = workbook.getSheetAt(0);// 获取第一个表
                // 如果已有行、列直接获取
                fileOut = new FileOutputStream(itemInfo.getFilePath());
                Map<Integer, String> valueMap = getFillExcelMap(info, taskType);
                XSSFRow row = sheet.getRow(itemInfo.getRowIndex());// 得到行
                for (Integer integer : valueMap.keySet()) {
                    XSSFCellStyle cellStyle = workbook.createCellStyle();
                    XSSFCell cell = row.createCell(integer);
                    cell.setCellStyle(cellStyle);
                    cell.setCellValue(valueMap.get(integer));// 写数据
                }
                workbook.write(fileOut);
                fileOut.flush();
                fileOut.close();
                inputStream.close();
                return info;
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {

            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                throwable.printStackTrace();
            }
        }, new Action() {
            @Override
            public void run() throws Exception {
            }
        });
    }

    private Map<Integer, String> getFillExcelMap(TaskItemInfo itemInfo, int taskType) {
        Map<Integer, String> valueMap = new HashMap<>();
        switch (taskType) {
            case 0:
                valueMap.put(9, itemInfo.getTestDate());
                valueMap.put(10, itemInfo.getTestDevice());
                valueMap.put(11, itemInfo.getTestValue());
                if (itemInfo.getIsLeak().equals("1")) {
                    valueMap.put(12, "是");
                    valueMap.put(13, itemInfo.getLeakLocation());

                } else {
                    valueMap.put(12, "否");
                    valueMap.put(13, "");
                }
                valueMap.put(14, itemInfo.getBackGround());
                valueMap.put(18, itemInfo.getTestPerson());
                break;
            case 1:
                valueMap.put(15, itemInfo.getRepeatDate());
                valueMap.put(16, itemInfo.getRepeatDevice());
                valueMap.put(17, itemInfo.getRepeatValue());
                valueMap.put(18, itemInfo.getBackGround());
                valueMap.put(21, itemInfo.getRepeatPerson());
                break;
            case 2:
                valueMap.put(10, itemInfo.getTestDate());
                valueMap.put(11, itemInfo.getTestDevice());
                valueMap.put(12, itemInfo.getTestValue());
                valueMap.put(15, itemInfo.getBackGround());
                valueMap.put(16, itemInfo.getTestPerson());
                break;
        }
        return valueMap;
    }
}
